package pt.fct.mpc.lastfm.album;

import java.io.IOException;
import java.net.MalformedURLException;
import java.util.List;

import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;

import pt.fct.mpc.lastfm.R;
import pt.fct.mpc.lastfm.connectivity.LastfmBluetoothUnavailableException;
import pt.fct.mpc.lastfm.connectivity.LastfmURL;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class AlbumTracksActivity extends Activity {

	private static final String TAG = "Last.fm Album Activity";
	AlbumData albumDataSet;
	boolean gotTracks = false;
	ArrayAdapter<String> tracksArrayAdapter;

	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.list);

		// Reads the info sent by the intent
		Intent the_intent_that_gave_life_to_everything = getIntent();
		Bundle intent_data = the_intent_that_gave_life_to_everything
				.getExtras();

		if (intent_data == null) {
			Log.e(TAG, "No extra data");
			finish();
		}

		albumDataSet = (AlbumData) intent_data.getSerializable("albumserial");

		if (albumDataSet == null) {
			Log.e(TAG, "Null values from bundle: albumDataSet = "
					+ albumDataSet);
			finish();
		}

		if (!gotTracks)
			getTracks();
		
		createTracksTab();
	}
	

	private void getTracks() {
		String search_query = "?method=playlist.fetch"
				+ "&playlistURL=lastfm://playlist/album/"
				+ albumDataSet.getId() + "&api_key="
				+ getString(R.string.lastfm_api_key);
	
		String request_str = getString(R.string.lastfm_api_url) + search_query;
	
		LastfmURL request_url = null;
		try {
			request_url = new LastfmURL(request_str);
		} catch (MalformedURLException e3) {
			Log.d("ArtistActivity.getArtistInfo", "MalformedURLException 143");
		}
	
		SAXParserFactory spf = SAXParserFactory.newInstance();
		SAXParser sp = null;
		try {
			sp = spf.newSAXParser();
		} catch (ParserConfigurationException e2) {
			Log.d("AlbumActivity.getAlbumInfo",
					"ParserConfigurationException 151");
		} catch (SAXException e2) {
			Log.d("AlbumActivity.getAlbumInfo", "SAXException 153");
		}
	
		XMLReader xr = null;
		try {
			xr = sp.getXMLReader();
		} catch (SAXException e1) {
			Log.d("AlbumActivity.getAlbumInfo", "SAXException 160");
		}
	
		TracksXmlHandler myTracksXmlHandler = new TracksXmlHandler();
		xr.setContentHandler(myTracksXmlHandler);
	
		try {
			xr.parse(new InputSource(request_url.openStream()));
		} catch (IOException e) {
			Log.d("ArtistActivity.getArtistInfo", "IOException 169");
		} catch (SAXException e) {
			Log.d("ArtistActivity.getArtistInfo", "SAXException 171");
		} catch (LastfmBluetoothUnavailableException e) {
			e.printStackTrace();
		}
	
		albumDataSet.setTracks(myTracksXmlHandler.getParsedData());
		this.gotTracks = true;
	}

	private void createTracksTab() {
		tracksArrayAdapter = new ArrayAdapter<String>(
				this, R.layout.list_item);
		ListView resultsListView = (ListView) findViewById(R.id.ListView);
		resultsListView.setAdapter(tracksArrayAdapter);

		List<TrackData> tracks = albumDataSet.getTracks();
		for (int i = 0; i < tracks.size(); i++) {
			int time = Integer.parseInt(tracks.get(i).getDuration());
			int min = time / 60000; // 60*1000
			int sec = (time / 1000) % 60;
			String secString = "" + sec;
			if (sec < 10)
				secString = "0" + sec;
			tracksArrayAdapter.add(i + 1 + ".  " + tracks.get(i).getName()
					+ "  " + min + ":" + secString);
		}
	}
}